<template>
  <div class="order-list-item" v-if="item.order_id">
    <div class="tit">
      <div class="time">{{ item.create_time }}</div>
      <div class="status">
        <span>{{ item.state_text }}</span>
      </div>
    </div>
    <div class="list">
      <div class="list-item" v-for="g in item.goods" :key="g.goods_id">
        <div class="goods-img">
          <img :src="g.goods_image" alt="">
        </div>
        <div class="goods-content text-ellipsis-2">
          {{ g.goods_name }}
        </div>
        <div class="goods-trade">
          <p>¥ {{ g.goods_price }}</p>
          <p>x {{ g.total_num }}</p>
        </div>
      </div>
    </div>
    <div class="total">
      共{{ item.total_num }}件商品，总金额 ¥{{ item.total_price }}
    </div>
    <div class="actions">
      <!-- 订单未支付的情况显示下面两个按钮 -->
      <!-- <span v-if="isPay">立刻付款</span> -->
      <span v-if="isPay" @click="cancelOrder">取消订单</span>
      <!-- 订单待发货和待收货的情况显示 -->
      <span v-if="isConfirm" @click="confirmOrder">确认收货</span>
      <span v-if="isComment">评价</span>
    </div>
  </div>
</template>

<script>
import { cancelOrder, confirmOrder } from '@/api/order'
export default {
  name: 'OrderListItem',
  props: {
    type: {
      type: String,
      default: 'all'
    },
    item: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    isPay () {
      return this.item.state_text === '待发货'
    },
    isConfirm () {
      return this.item.state_text === '待发货' || this.item.state_text === '待收货'
    },
    isComment () {
      return this.item.state_text === '待评价'
    }
  },
  methods: {
    cancelOrder () {
      this.$dialog.confirm({
        message: '您是否真的要取消订单？'
      })
        .then(async () => {
          // on confirm
          // 6636871
          await cancelOrder(this.item.order_id)
          this.$toast('取消订单成功')
          this.$emit('refreshOrders')
        })
        .catch(() => {
          // on cancel
        })
    },
    async confirmOrder () {
      await confirmOrder(this.item.order_id)
      this.$emit('refreshOrders')
    }
  }
}
</script>

<style lang="less" scoped>
.order-list-item {
  margin: 10px auto;
  width: 94%;
  padding: 15px;
  background-color: #ffffff;
  box-shadow: 0 0.5px 2px 0 rgba(0,0,0,.05);
  border-radius: 8px;
  color: #333;
  font-size: 13px;

  .tit {
    height: 24px;
    line-height: 24px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .status {
      color: #fa2209;
    }
  }

  .list-item {
    display: flex;
    .goods-img {
      width: 90px;
      height: 90px;
      margin: 0px 10px 10px 0;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .goods-content {
      flex: 2;
      line-height: 18px;
      max-height: 36px;
      margin-top: 8px;
    }
    .goods-trade {
      flex: 1;
      line-height: 18px;
      text-align: right;
      color: #b39999;
      margin-top: 8px;
    }
  }

  .total {
    text-align: right;
  }
  .actions {
    text-align: right;
    span {
      display: inline-block;
      height: 28px;
      line-height: 28px;
      color: #383838;
      border: 0.5px solid #a8a8a8;
      font-size: 14px;
      padding: 0 15px;
      border-radius: 5px;
      margin: 10px 5px;
      &:last-child{
        margin-right: 0;
      }
    }
  }
}
</style>
